package com.example.helper2;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

public class user_message_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<UserMsgInfo> mDataset;

    public user_message_adapter(List<UserMsgInfo> mDataset){
        this.mDataset = mDataset;
    }
    /**
     * 创建ViewHolder
     *
     * @param viewGroup
     * @param i
     * @return
     */
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i){
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.user_message_item, viewGroup, false);
        RecyclerView.ViewHolder vh = new user_message_viewholder(v, new MyViewHolderClick() {
            @Override
            public void onLikeClick(final int id,final int position) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String url_s = "http://wolfdnd.top:2020/";
                            URL url = new URL(url_s);
                            HttpURLConnection mypost = (HttpURLConnection)url.openConnection();
                            mypost.setRequestMethod("POST");
                            mypost.setRequestProperty("Charset","UTF-8");
                            mypost.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                            mypost.setConnectTimeout(5000);
                            mypost.setDoOutput(true);

                            String behavior = "like";
                            String ID = id + "";
                            String data = "id="+ URLEncoder.encode(ID)+"&behavior="+URLEncoder.encode(behavior);
                            mypost.setRequestProperty("Content-Length",data.length()+"");
                            mypost.connect();
                            OutputStream out = mypost.getOutputStream();
                            out.write(data.getBytes());
                            out.flush();
                            out.close();

                            int code = mypost.getResponseCode();
                            if(code == 200 && (url.toString().equals(mypost.getURL().toString()))){
                                Log.v("点赞是否成功","点赞成功");
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
                mDataset.get(position).setNum(mDataset.get(position).getNum()+1);
                notifyItemChanged(position);
            }
        });
        return vh;
    }
    /**
     * 通过ViewHolder对item中的控件进行控制（如：显示数据等等）
     *
     * @param viewHolder
     * @param i
     */
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        user_message_viewholder uvh = (user_message_viewholder)viewHolder;
        uvh.user_name.setText(mDataset.get(i).getUser_name());
        uvh.user_word.setText(mDataset.get(i).getUser_word());
        uvh.user_msg.setText(mDataset.get(i).getUser_msg());
        uvh.like_num.setText(mDataset.get(i).getNum()+"");

        uvh.bind(mDataset.get(i));

    }

    public int getItemCount() {
        return mDataset.size();
    }


    // 点击事件的接口
    private interface MyViewHolderClick{
        public void onLikeClick(int id, int position);
    }

    // 创建viewholder内部类
    public class user_message_viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView user_name;
        public TextView user_word;
        public TextView user_msg;
        public TextView like_num;
        public ImageButton like_icon_off;
        public MyViewHolderClick myViewHolderClick;

        public user_message_viewholder(View view, MyViewHolderClick listener){
            super(view);
            user_name = view.findViewById(R.id.user_name);
            user_word = view.findViewById(R.id.user_word);
            user_msg = view.findViewById(R.id.user_msg);
            like_num = view.findViewById(R.id.likes_num);
            like_icon_off = view.findViewById(R.id.like_icon_off);
            this.myViewHolderClick = listener;
            like_icon_off.setOnClickListener(this);
        }
        public void bind(UserMsgInfo userMsgInfo){
            like_icon_off.setTag(userMsgInfo);
        }
        public void onClick(View view){
            myViewHolderClick.onLikeClick(((UserMsgInfo)like_icon_off.getTag()).getId(),getLayoutPosition());
        }
    }
}
